package com.pengtu.gsj.dao.system;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.google.common.collect.Lists;
import com.pengtu.gsj.dao.hibernate.HibernateDao;
import com.pengtu.gsj.entity.app.Attachment;

/** 
 * @author zl 
 * @version 创建时间：2017年4月5日 上午9:54:48 
 * 类说明 
 */
@Repository
public class AttachmentDao  extends HibernateDao<Attachment, String>{

	public List<Attachment> getAttachmentsByPartId(String partId) {
		if(StringUtils.isEmpty(partId)){
			return Lists.newArrayList();
		}else{
			String hql = "select t from Attachment t where t.delFlag='0' " +
					"and t.partId =?  order by t.sortNo ASC,t.createDate ASC";
			return find(hql,partId);}
	}

	/**
	 * 通过tableId 查询数据 tableId 一般为业务表格的ID
	 * getAttachmentList:
	 * 适用:
	 * @param tableId
	 * @param tableField
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public List<Attachment> getAttachmentList(String tableId, String tableField) {
		if(StringUtils.isEmpty(tableId)){
			return Lists.newArrayList();
		}
		if(StringUtils.isNotEmpty(tableField)){
			String hql = "select t from Attachment t where t.delFlag='0' " +
					"and t.tableId =? and t.tableField=? order by t.createDate ASC";
			return find(hql,tableId,tableField);
		}else{
			String hql = "select t from Attachment t where t.delFlag='0' " +
					"and t.tableId =?  order by t.createDate ASC";
			return find(hql,tableId);
		}
	}
	
	public List<Attachment> getAttachmentListByTableId(String tableId, String partId) {
		String hql = "select t from Attachment t where t.delFlag='0' " +
				"and t.tableId =? and t.partId=? order by t.createDate ASC";
		return find(hql,tableId,partId);
	}

	public List<Attachment> getAttachmentList(String tableId) {
		return this.getAttachmentList(tableId, null);
	}

	public int updateTableId(String oldId, String newId) {
		String hql="update Attachment t set t.tableId=? where t.tableId=?";
		return batchExecute(hql,newId,oldId);
	}

	public List<Attachment> getAttachmentsByTabelName(String tableName) {
		String hql = "from Attachment t where t.tableName=?";
		return find(hql, tableName);
	}
	
	public List<Attachment> getTableIdByPartId(String partId){
		String hql = "select new com.pengtu.gsj.entity.app.Attachment(t.tableId, s.orgName) from Attachment t , Organization s where t.partId =? and t.tableId = s.id GROUP BY t.tableId, s.orgName";
		return find(hql, partId);
	}
	
	public List<Attachment> getAttachmentYears() {
		String hql = "select new com.pengtu.gsj.entity.app.Attachment(t.year) from Attachment t  where t.delFlag = '0' group by t.year ";
		return find(hql);
	}
	
	 

}
